package com.kcnet.qk.controller.sjgl;

import android.graphics.Rect;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;
import org.w3c.dom.Node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by czc on 2017/7/4.
 * 发起群聊
 */

public class SjqlLaunchGroupChatController extends BasePeopleController {

    private int mType;
    private String mClassName;
    private static String TAG = "发起群聊";

    private static SjqlLaunchGroupChatController mRongCloudInstance;

    private boolean isJoinSelect;
    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());


    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static SjqlLaunchGroupChatController getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (SjqlLaunchGroupChatController.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new SjqlLaunchGroupChatController();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) {
        mType = event.getEventType();
        mClassName = (String) event.getClassName();
        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.LauncherUI)) {
            NLogger.d(TAG, "1.进入微信首页 点击 通讯录 按钮");
            sleepTime(2000);
            RootShell.click(471,56);
            NLogger.d(TAG, "2.点击群聊");
            sleepTime(2000);
            tagTextClick("发起群聊");
            isJoinSelect =true;
           // RootShell.click(400,100);
        }  else if (isJoinSelect &&(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.SelectContactUI)
                ||mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.widget.ListView"))) {
            NLogger.d(TAG, "4.选择好友建立群聊界面，点击搜索框");
            beginAddPeople();
        } else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.ChattingUI)){
            NLogger.d(TAG, "6 进入聊天界面 点击右上角");
            sleepTime(1000);
            RootShell.click(456,38);
        }
        else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.ChatroomInfoUI)){
            NLogger.d(TAG, "7 进入群详情界面");
            sleepTime(1000);
            RootShell.swipe(400,900,300,300);
            sleepTime(2000);
            RootShell.swipe(400,900,300,300);
            sleepTime(1000);
          //  AccessibilityNodeInfo node=  findTextNodeInfo("保存到通讯录");
            NLogger.d(TAG, "8 点击保存到通讯录");
            RootShell.click(450,305);
            stopTask();
        }

    }

    private void beginAddPeople() {
        AccessibilityNodeInfo scrllorNodeInfo;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                Log.i("czc", "beginAddPeople");
                scrllorNodeInfo = NodeUtil.findNodeByClassNameAndTime(root, Const.LIST_VIEW, 1);
                if (scrllorNodeInfo == null) {
                    Log.i("czc", "scrllorNode is null ");
                }

                List<AccessibilityNodeInfo> nodeByClassList = NodeUtil.findNodeByClassList(scrllorNodeInfo, Const.RELATIVE_LAYOUT);//获取ListView下的 RELATIVE_LAYOUT
                Log.i(TAG, Integer.toString(nodeByClassList.size()));
                for (int i = 0; i < nodeByClassList.size(); i++) {
                    //判断任务是否完成
                    if (currentAddCount >= maxAddCount) {
                        currentAddCount = 0;
                        SubTask();
                        break;
                    }
                    AccessibilityNodeInfo nodeInfo = nodeByClassList.get(i);
                    List<AccessibilityNodeInfo> NodeList_list2 = NodeUtil.traverseNodeByClassList(nodeInfo, Const.TEXT_VIEW);
                    if (nodeInfo.getChildCount() == 2 && NodeList_list2.size() > 0) {//判断是否有子项，有则为有名称
                        String name = NodeList_list2.get(0).getText().toString();
                        if (!mRecordMap.containsKey(name)) {
                            mRecordMap.put(name, true);
                            currentAddCount++;
                            NodeUtil.performClick(nodeInfo);
                        }
                    }

                    if (i == nodeByClassList.size() - 1) {
                        if (scrllorNodeInfo != null) {
                            Log.i(TAG, "scrllor");
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }
            }
        }

    }

    public void SubTask() {
        NLogger.d(TAG, "7 提交");
        tagIdClick("com.tencent.mm:id/hg");//提交的ID不会变
        mRecordMap.clear();
        isJoinSelect = false;
      //  stopTask();
    }


    /**
     *
     * @param maxAddCount
     * @param minAddTime
     * @param maxAddTime
     */
    public void setParms(int maxAddCount, int minAddTime, int maxAddTime) {
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        mRecordMap.clear();

    }
}
